﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"

void Setpos(short x, short y)//改变光标位置封装函数
{
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos1 = { x,y };//定位光标位置
	SetConsoleCursorPosition(houtput, pos1);//改变光标位置
}
void WelcomeToGame()//欢迎界面
{
	Setpos(40, 14);
	wprintf(L"欢迎来到cbh的贪吃蛇\n");
	Setpos(42, 25);
	system("pause");
	system("cls");//清屏
	Setpos(30, 14);
	wprintf(L"用↑.↓.←.→控制移动,F3加速.F4减速\n");
	Setpos(42, 16);
	wprintf(L"加速得高分\n");
	Setpos(42, 25);
	system("pause"); 
	system("cls");

}

void CreatMap()//绘制地图
{
	//上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", wall);
	}
	//下
	Setpos(0, 28);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", wall);
	}
	//左
	for (i = 0; i < 28; i++)
	{
		Setpos(0, i);
		wprintf(L"%lc", wall);
	}
	//右
	for (i = 0; i < 28; i++)
	{
		Setpos(56, i);
		wprintf(L"%lc", wall);
	}
	
}

void InitSnake(pSnake ps)//创建蛇
{
    int i=0;
	pSnakeNode cur=NULL;
	for (int i = 0;i < 5;i++)//申请5个空间
	{
	    cur=(pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->next=NULL;
		cur->x= pos_x+2*i;
		cur->y=pos_y;

		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next=ps->_pSnake;
			ps->_pSnake=cur;
		}

	}

	cur=ps->_pSnake;
	while (cur)
	{
	Setpos(cur->x,cur->y);
	wprintf(L"%lc", body);
		cur=cur->next;
	}

	//蛇属性
	ps->dir=right;
	ps->score=0;
	ps->food_goal=1;
	ps->sleep_time=200;//毫秒
	ps->sta=ok;
}

void CreateFood(pSnake ps)//创建食物
{
    int x=0;
	int y=0;
	//x:2~54
	//y:1~25
	//食物不和蛇身冲突
	again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	}while(x%2!=0);
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
		cur=cur->next;
	}
	//创建食物节点
	pSnakeNode pFood = (pSnakeNode) malloc(sizeof(SnakeNode));
	if (pFood==NULL)
	{
		perror("InitSnake()::malloc()");
		return;
	}
	pFood->x=x;
	pFood->y = y;
	pFood->next=NULL;

	Setpos(x,y);
	wprintf(L"%lc",foods);

	ps->pFood=pFood;
}
void startGame(pSnake ps)//初始化
{
	//隐藏光标
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO curfor_info ;
	GetConsoleCursorInfo(houtput, &curfor_info);
	curfor_info.bVisible = false;
	SetConsoleCursorInfo(houtput, &curfor_info);
	//欢迎界面
	WelcomeToGame();
	//绘制地图
	CreatMap();
	//创建蛇
	InitSnake(ps);
	//创建食物
	CreateFood(ps);
	

}

void PrintHelpInfo()
{
     Setpos(64,10);
	 wprintf(L"不能穿墙或自残");
	 Setpos(64, 11);
	 wprintf(L"用↑.↓.←.→控制移动,F3加速.F4减速\n");
	 Setpos(64, 12);
	 wprintf(L"加速得分高");
	 Setpos(64, 13);
	 wprintf(L"ESC退出游戏，空格暂停游戏");
	 


}

#define KEY_PRESS(vk) ((GetAsyncKeyState(vk)&1)?1:0)


void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
   }
}

int NextIsFood(pSnakeNode pn, pSnake ps)//判断食物
{
    return (ps->pFood->x == pn->x && ps->pFood->y == pn->y);
	
}

void EatFood(pSnakeNode pn, pSnake ps)//吃掉
{
    ps->pFood->next=ps->_pSnake;
	ps->_pSnake=ps->pFood;
	free(pn);
	pn=NULL;
	pSnakeNode cur=ps->_pSnake;
	while (cur)
	{
		Setpos(cur->x,cur->y);
		wprintf(L"%lc",body);
		cur=cur->next;
	}
	ps->score+=ps->food_goal;
	

	CreateFood(ps);
}

void NoFood(pSnakeNode pn, pSnake ps)
{
    pn->next=ps->_pSnake;
	ps->_pSnake=pn;

	pSnakeNode cur=ps->_pSnake;
	while (cur->next->next != NULL)
	{
	Setpos(cur->x,cur->y);
	wprintf(L"%lc",body);
		cur=cur->next;
	}
  
  Setpos(cur->next->x,cur->next->y);
  printf("  ");

  free(cur->next);
  cur->next=NULL;
}

void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 || ps->_pSnake->y == 0 || ps->_pSnake->y == 28)
	{
	   ps->sta=kill_by_wall;
	}
}

void KillBySelf(pSnake ps)//撞自己
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
		  ps->sta=kill_by_self;
		  break;
		}
		cur=cur->next;
	}
}

void SnakeMove(pSnake ps)//走一步
{
    pSnakeNode pNextNode=(pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::mallol()");
	}
	switch (ps->dir)
	{
		case up:pNextNode->x=ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y-1;
		    break;
		case down:pNextNode->x = ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y + 1;
			break;
		case left:pNextNode->x = ps->_pSnake->x-2;
			pNextNode->y = ps->_pSnake->y ;
			break;
		case right:pNextNode->x = ps->_pSnake->x+2;
			pNextNode->y = ps->_pSnake->y ;
			break;
	}
	if (NextIsFood(pNextNode, ps))
	{
	     EatFood(pNextNode,ps);

	}
	else
	{
	    NoFood(pNextNode, ps);
	}

	KillByWall(ps);
	KillBySelf(ps);


}
void runGame(pSnake ps)//运行游戏
{
   //打印帮助信息
   PrintHelpInfo();
   do
   {
       //打印分数
	   Setpos(64,7);
	   printf("总分数：%d\n",ps->score);
	   Setpos(64, 8);
	   printf("当前食物数：%2d\n", ps->food_goal);

	   if (KEY_PRESS(VK_UP) && ps->dir != down)
	   {
		   ps->dir=up;
	   }
	   else if (KEY_PRESS(VK_DOWN) && ps->dir != up)
	   {
		   ps->dir=down;
	   }
	   else if (KEY_PRESS(VK_LEFT) && ps->dir != right)
	   {
		   ps->dir = left;
	   }
	   else if (KEY_PRESS(VK_RIGHT) && ps->dir != left)
	   {
		   ps->dir = right;
	   }
	   else if (KEY_PRESS(VK_SPACE))//暂停
	   {
	       Pause();
	   }
	   else if (KEY_PRESS(VK_F3))//加速
	   {
		   if (ps->sleep_time > 80)
		   {
			   ps->sleep_time -= 30;
			   ps->food_goal += 2;
		   }
	   }
	   else if (KEY_PRESS(VK_F4))//减速
	   {
		   if (ps->food_goal > 2)
		   {
			   ps->sleep_time += 30;
			   ps->food_goal -= 2;
		    }
	   }
	   else if (KEY_PRESS(VK_ESCAPE))//退出
	   {
	       ps->sta=end;
	   }

	   SnakeMove(ps);
	   Sleep(ps->sleep_time);

   }while(ps->sta==ok);
}

void endgame(pSnake ps)//善后
{
    Setpos(24,12);
	switch (ps->sta)
	{
	case end:
	printf("已结束");
		break;
	case kill_by_self:
		printf("自杀,已结束");
			break;
     case kill_by_wall:
		printf("撞墙,已结束");
			break;
	}

	Setpos(0,28);
	pSnakeNode cur=ps->_pSnake;
	while (cur)
	{   
		pSnakeNode der=cur;
		cur=cur->next;
		free(der);
	}


}